/* Microsoft Reference Implementation for TPM 2.0
 *
 *  The copyright in this software is being made available under the BSD License,
 *  included below. This software may be subject to other third party and
 *  contributor rights, including patent rights, and no such rights are granted
 *  under this license.
 *
 *  Copyright (c) Microsoft Corporation
 *
 *  All rights reserved.
 *
 *  BSD License
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this list
 *  of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright notice, this
 *  list of conditions and the following disclaimer in the documentation and/or
 *  other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "Tpm.h"
#include "PCR_Read_fp.h"

#if CC_PCR_Read  // Conditional expansion of this file

/*(See part 3 specification)
// Read a set of PCR
*/
/*
This command returns the values of all PCR specified in pcrSelectionIn.
The TPM will process the list of TPMS_PCR_SELECTION in pcrSelectionIn in order. Within each 
TPMS_PCR_SELECTION, the TPM will process the bits in the pcrSelect array in ascending PCR order
(see TPM 2.0 Part 1, Selecting Multiple PCR). If a bit is SET, and the indicated PCR is present, 
then the TPM will add the digest of the PCR to the list of values to be returned in pcrValues.
The TPM will continue processing bits until all have been processed or until pcrValues would be 
too large to fit into the output buffer if additional values were added.
The returned pcrSelectionOut will have a bit SET in its pcrSelect structures for each value present 
in pcrValues.
The current value of the PCR Update Counter is returned in pcrUpdateCounter.
The returned list may be empty if none of the selected PCR are implemented.
NOTE If no PCR are returned from a bank, the selector for the bank will be present in pcrSelectionOut.
No authorization is required to read a PCR and any implemented PCR may be read from any locality

此命令返回 pcrSelectionIn 中指定的所有 PCR 的值。
TPM 将按顺序处理 pcrSelectionIn 中的 TPMS_PCR_SELECTION 列表。 在每个 TPMS_PCR_SELECTION 中，TPM 将按 PCR 
升序处理 pcrSelect 数组中的位
（参见 TPM 2.0 第 1 部分，选择多个 PCR）。 如果设置了一个位，并且存在指示的 PCR，则 TPM 会将 PCR 的摘要添加到要在 
pcrValues 中返回的值列表中。
TPM 将继续处理位，直到所有位都已处理或直到 pcrValues 太大而无法放入输出缓冲区（如果添加了额外的值）。
返回的 pcrSelectionOut 将在其 pcrSelect 结构中为 pcrValues 中存在的每个值设置一个位。
PCR 更新计数器的当前值在 pcrUpdateCounter 中返回。
如果选定的 PCR 均未实现，则返回的列表可能为空。
注意如果没有 PCR 从银行返回，银行的选择器将出现在 pcrSelectionOut 中。
读取 PCR 不需要授权，任何实施的 PCR 都可以从任何地方读取
*/
TPM_RC
TPM2_PCR_Read(
    PCR_Read_In     *in,            // IN: input parameter list
    PCR_Read_Out    *out            // OUT: output parameter list
    )
{
// Command Output

    // Call PCR read function.  input pcrSelectionIn parameter could be changed
    // to reflect the actual PCR being returned
    PCRRead(&in->pcrSelectionIn, &out->pcrValues, &out->pcrUpdateCounter);

    out->pcrSelectionOut = in->pcrSelectionIn;

    return TPM_RC_SUCCESS;
}

#endif // CC_PCR_Read